from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import uvicorn
import json
import argparse

app = FastAPI()

@app.get("/")
def root():
    return {"欢迎来到": "FastAPI"}

@app.post("/checkPyConnection")
async def checkPyConnection():
    return json.dumps({'flag': True, 'msg': 'python server has started'})

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('port',type=str,nargs='?',const=1, default='8087')
    args=parser.parse_args()
    # origins = ["*"]
    origins = [
        "http://localhost:" + args.port
    ]
    app.add_middleware(
        CORSMiddleware,
        allow_origins=origins,
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
    uvicorn.run(app, host='0.0.0.0', port=int(args.port))